package org.gdpu.model;

import org.gdpu.util.ChangeTime;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 考试
 */
@Entity
public class Examination {
    /**
     * 系统序号
     */
    private int id;
    /**
     * 考试名
     */
    private String name;
    /**
     * 考试描述
     */
    private String description;
    /**
     * 考试开始时间
     */
    private Date examBeginTime;
    /**
     * 考试结束时间
     */
    private Date examEndTime;
    /**
     * 允许提前交卷时间
     */
    private Date advanceTime;
    /**
     * 考试时长
     */
    private Date time;
    /**
     * 考试用户
     */
    private Set<Examiner> examiners = new HashSet<Examiner>(0);
    /**
     * 考试类型
     */
    private ExaminationType type;
    /**
     * 考试形式
     */
    private ExaminationMode mode;
    /**
     * 是否公开试卷
     */
    private boolean openTestPaper = false;
    /**
     * 是否公开成绩
     */
    private boolean openScore = false;
    /**
     * 考试状态
     */
    private ExaminationState examinationState;
    /**
     * 课程
     */
    private Subject subject;
    /**
     * 组别
     */
    private Teams team;
    /**
     * 考试试卷
     */
    private TestPaperInfo testPaperInfo;
    /**
     * 拥有者
     */
    private String owner;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getExamBeginTime() {
        return examBeginTime;
    }

    public void setExamBeginTime(Date examTime) {
        this.examBeginTime = examTime;
    }

    @Column(nullable = false)
    public Date getExamEndTime() {
        return examEndTime;
    }

    public void setExamEndTime(Date examEndTime) {
        this.examEndTime = examEndTime;
    }

    @Temporal(TemporalType.TIME)
    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    @Temporal(TemporalType.TIME)
    public Date getAdvanceTime() {
        return advanceTime;
    }

    public void setAdvanceTime(Date advanceTime) {
        this.advanceTime = advanceTime;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "examination")
    public Set<Examiner> getExaminers() {
        return examiners;
    }

    public void setExaminers(Set<Examiner> persons) {
        this.examiners = persons;
    }

    public boolean isOpenTestPaper() {
        return openTestPaper;
    }

    public void setOpenTestPaper(boolean openTestPaper) {
        this.openTestPaper = openTestPaper;
    }

    public boolean isOpenScore() {
        return openScore;
    }

    public void setOpenScore(boolean openScore) {
        this.openScore = openScore;
    }

    @Enumerated(EnumType.ORDINAL)
    @Column(length = 3, nullable = false, columnDefinition = "INT default 0")
    public ExaminationType getType() {
        return type;
    }

    public void setType(ExaminationType type) {
        this.type = type;
    }

    @Enumerated(EnumType.ORDINAL)
    @Column(length = 3, nullable = false, columnDefinition = "INT default 0")
    public ExaminationMode getMode() {
        return mode;
    }

    public void setMode(ExaminationMode mode) {
        this.mode = mode;
    }

    @Enumerated(EnumType.ORDINAL)
    @Column(length = 3, nullable = false, columnDefinition = "INT default 0")
    public ExaminationState getExaminationState() {
        return examinationState;
    }

    public void setExaminationState(ExaminationState examinationState) {
        this.examinationState = examinationState;
    }

    @ManyToOne
    @JoinColumn(name = "subjectId")
    public Subject getSubject() {
        return subject;
    }

    public void setSubject(Subject subject) {
        this.subject = subject;
    }

    @ManyToOne
    @JoinColumn(name = "teamName")
    public Teams getTeam() {
        return team;
    }

    public void setTeam(Teams team) {
        this.team = team;
    }

    @ManyToOne
    @JoinColumn(name = "testPaper")
    public TestPaperInfo getTestPaperInfo() {
        return testPaperInfo;
    }

    public void setTestPaperInfo(TestPaperInfo testPaperInfo) {
        this.testPaperInfo = testPaperInfo;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    @Override
    public String toString() {
        String temp = "";
        if ("".equals(ChangeTime.formatTime2(advanceTime)) || "00:00:00".equals(ChangeTime.formatTime2(advanceTime)))
            temp = "可随时交卷";
        else {
            String info[] = ChangeTime.formatTime2(advanceTime).split(":");
            temp += info[0].equals("00") ? "" : info[0] + "小时";
            temp += info[1].equals("00") ? "" : info[1] + "分钟";
            temp += info[2].equals("00") ? "" : info[2] + "秒";
            temp += "后允许交卷";
        }

        return "考试名称：" + name + "<br/>考试描述：" + description + "<br/>课程:" + subject.getSubjectName() + "<br/>考试班级：" + team.getTeamName() +
                "<br/>考试开始时间：" + ChangeTime.formatWholeDate(examBeginTime) + "<br/>考试结束时间:" + ChangeTime.formatWholeDate(examEndTime) +
                "<br/>" + temp + "<br/>考试时长:" + ChangeTime.formatTime2(time) + "<br/>考试类型:" +
                type.toString() + "<br/>考试形式:" + mode.toString() + "<br/>";
    }
}




